package zabbix

import (
	"encoding/json"
	"errors"
)

func ProxyCreateData() (string,map[string]interface{}) {
	method := "proxy.create"
	var data map[string]interface{}
	jsonstr := []byte(`{
        "host": "Passive proxy",
        "status": "6",
        "interface": {
            "ip": "127.0.0.1",
            "dns": "",
            "useip": "1",
            "port": "10051"
        },
        "hosts": [
            {
                "hostid": "10192"
            },
            {
                "hostid": "10139"
            }
        ]
	}`)
	json.Unmarshal(jsonstr,&data)
	return method,data
}


func ProxyDeleteData() (string,map[string]interface{}) {
	method := "proxy.delete"
	var data map[string]interface{}
	jsonstr := []byte(`[
        "10286",
        "10285"
	]`)
	json.Unmarshal(jsonstr,&data)
	return method,data
}


func ProxyUpdateData() (string,map[string]interface{}) {
	method := "proxy.update"
	var data map[string]interface{}
	jsonstr := []byte(`{
        "proxyid": "10293",
        "hosts": [
            "10294",
            "10295"
        ]
	}`)
	json.Unmarshal(jsonstr,&data)
	return method,data
}


func ProxyGetData() (string,map[string]interface{}) {
	method := "proxy.get"
	var data map[string]interface{}
	jsonstr := []byte(`{
        "output": "extend",
        "selectInterface": "extend"
	}`)
	json.Unmarshal(jsonstr,&data)
	return method,data
}


func ProxyCreate(url string,user string,password string) (interface{},*API) {
	api := LoginAndHandle(url,user,password)
	method,data := ProxyCreateData()
	result,err:=api.ZabbixRequest(method,data)
	if err != nil {
		return nil,api
	}
	if result.Error.Code != 0 {
		err = errors.New(result.Error.Message)
		return nil,api
	}
	resultdata := result.Result
	return resultdata,api
}

func ProxyDelete(url string,user string,password string) (interface{},*API) {
	api := LoginAndHandle(url,user,password)
	method,data := ProxyDeleteData()
	result,err:=api.ZabbixRequest(method,data)
	if err != nil {
		return nil,api
	}
	if result.Error.Code != 0 {
		err = errors.New(result.Error.Message)
		return nil,api
	}
	resultdata := result.Result
	return resultdata,api
}

func ProxyUpdate(url string,user string,password string) (interface{},*API) {
	api := LoginAndHandle(url,user,password)
	method,data := ProxyUpdateData()
	result,err:=api.ZabbixRequest(method,data)
	if err != nil {
		return nil,api
	}
	if result.Error.Code != 0 {
		err = errors.New(result.Error.Message)
		return nil,api
	}
	resultdata := result.Result
	return resultdata,api
}

func ProxyGet(url string,user string,password string) (interface{},*API) {
	api := LoginAndHandle(url,user,password)
	method,data := ProxyGetData()
	result,err:=api.ZabbixRequest(method,data)
	if err != nil {
		return nil,api
	}
	if result.Error.Code != 0 {
		err = errors.New(result.Error.Message)
		return nil,api
	}
	resultdata := result.Result
	return resultdata,api
}



